/*
 * Copyright (c) 2005
 * XDoclet Team
 * All rights reserved.
 */
package org.xdoclet.plugin.qtags.impl;

import java.net.URL;

import org.xdoclet.QDoxMetadataProvider;
import org.xdoclet.plugin.qtags.impl.test.FooBarOnceTagImpl;
import org.xdoclet.plugin.qtags.impl.test.FooBarOnceTagNewImpl;

import junit.framework.TestCase;

/**
 * @author Diogo Quintela
 */
public class QTagOldOnceImplTestCase extends TestCase {
    protected URL getResourceRelativeToThisPackage(String resourceName) {
        String className = getClass().getName();
        String packageName = className.substring(0, className.lastIndexOf('.'));
        String resourcePath = "/" + packageName.replace('.', '/' ) + "/" + resourceName;
        URL resource = getClass().getResource(resourcePath);
        assertNotNull("Resource not found at path: " + resourcePath, resource);
        return resource;
    }

    public void testOldQTagOnceFailingImpl() {
        URL testSource = getResourceRelativeToThisPackage("test/BeanUsingOnceQTags.java");
        QDoxMetadataProvider metadataProvider = new QDoxMetadataProvider(testSource, Boolean.TRUE);
        metadataProvider.getDocletTagFactory().registerTag(FooBarOnceTagImpl.NAME, FooBarOnceTagImpl.class);

        metadataProvider.start();
        metadataProvider.getMetadata();
        // This should have failed because BeanUsingOnceQTags.java
        // declares @foo.bar-once more than one
        metadataProvider.stop();
    }

    public void testNewQTagOnceFailingImpl() {
        URL testSource = getResourceRelativeToThisPackage("test/BeanUsingOnceQTags.java");
        QDoxMetadataProvider metadataProvider = new QDoxMetadataProvider(testSource, Boolean.TRUE);
        metadataProvider.getDocletTagFactory().registerTag(FooBarOnceTagNewImpl.NAME, FooBarOnceTagNewImpl.class);

        try {
            metadataProvider.start();
            metadataProvider.getMetadata();
            fail("This should have failed because BeanUsingOnceQTags.java declares @foo.bar-once more than one");
        } catch (RuntimeException rex) {
            // OK. It's failing
            assertTrue(rex.getMessage().indexOf("is allowed only once") > -1);
            System.out.println("As expected, parsing of BeanUsingOnceQTags is failing");
        } finally {
            metadataProvider.stop();
        }
    }
}
